﻿using KTD.Web.Models;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XXS.Common.BaseDataAccessLayer;
using XXS.Common.Paginate;

namespace KTD.Web.BLL
{
    public class AreaManager
    {

        private readonly IBaseDAL<Area> _AreaDal;
        public AreaManager()
        {
            _AreaDal = new BaseDAL<Area>(new DB());
        }

        /// <summary>
        /// 获取区域列表
        /// </summary>
        /// <param name="im"></param>
        /// <returns></returns>
        public virtual IPaginate<AreaVD> GetListBy(AreaQD im)
        {
            var res = _AreaDal.FindList();

            if (im.isSon == "false")
            {
                res = res.Where(n => n.ison == false);
            }
            else
            {
                res = res.Where(n => n.ison == true);
            }
            if (!string.IsNullOrEmpty(im.Query))
            {
                res = res.Where(n => n.name.Contains(im.Query) || n.firstname.Contains(im.Query));
            }
            if (!string.IsNullOrEmpty(im.CityName))
            {
                var city = _AreaDal.FindList(n => n.name == im.CityName).FirstOrDefault();
                if (city != null)
                {
                    res = res.Where(n=>n.uniacid==city.Id);
                }
               
            }
            if (im.ParentId!=null)
            {
                res = res.Where(n=>n.uniacid==im.ParentId);
            }


            var list = res.OrderByDescending(n => n.CreateDateTime)
                .ToPaginate(im, n => new AreaVD().MapFrom(n));
            return list;
        }

        public virtual IEnumerable<SelectListItem> GetSelect()
        {
            var res = _AreaDal.FindList(n => n.ison == false);

            return res.ToList().Select(n => new SelectListItem { Text = n.name, Value = n.Id .ToString()}).ToList();
        }
        public virtual IEnumerable<AreaVD> GetCityListBy()
        {
            var res = _AreaDal.FindList(n=>n.ison==false);
            
            var list = res.OrderBy(n=>n.CreateDateTime).ToList().Select(n=>new AreaVD().MapFrom(n));
            return list;
        }
        /// <summary>
        /// 新建销售人员
        /// </summary>
        /// <param name="im"></param>
        public void Create(AreaCUD im)
        {
            Area model = new Area();
            im.MapTo(model);
            _AreaDal.Create(model);
        }

        /// <summary>
        /// 更新销售人员（审核）
        /// </summary>
        /// <param name="im"></param>
        public void Update(AreaCUD im)
        {
            Area model = _AreaDal.Find(im.Id);
            im.MapTo(model);
            _AreaDal.Update(model);
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AreaCUD Find(int id)
        {
            Area model = _AreaDal.Find(id);
            return new AreaCUD().MapFrom(model);
        }

        public AreaCUD FindByName(string AreaName)
        {
            Area model = _AreaDal.FindList(n=>n.name==AreaName).FirstOrDefault();
            return new AreaCUD().MapFrom(model);
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            Area model = _AreaDal.Find(id);
            if (model == null)
            {
                throw new Exception("错误的ID");
            }
            _AreaDal.Delete(model);
        }
    }
}